package com.yanqu.road.server.cmd.activity.musicroomrank;

import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.MusicRoomRankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.musicroomrank.CrossMusicRoomRankActivityMgr;
import com.yanqu.road.server.manager.activity.musicroomrank.MusicRoomRankActivity;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.AgentProtocol;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = CrossProtocol.C_MUSIC_ROOM_RANK_JOIN_SHOW, desc = "妙音坊冲榜 参赛")
public class CrossMusicRoomRankJoinShowCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        MusicRoomRankProto.CrossMusicRoomRankJoinShowReqMsg reqMsg = MusicRoomRankProto.CrossMusicRoomRankJoinShowReqMsg.parseFrom(packet.getBytes());

        CrossUserMgr.updateUserBaseInfo(packet.getPlayerId(), PlayerBasePb.parseToUserBaseInfo(reqMsg.getUserBaseInfo()));

        MusicRoomRankActivity activity = CrossMusicRoomRankActivityMgr.getActivity(reqMsg.getActivityId());
        if (activity != null) {
            MusicRoomRankProto.CrossMusicRoomRankJoinShowBackMsg.Builder respMsg =
                    activity.joinShow(packet.getPlayerId(), reqMsg.getRound(), reqMsg.getBeautyId(), reqMsg.getSkinId(), reqMsg.getAttrValueList(), reqMsg.getCardUsedAmount(), reqMsg.getCost());
            respMsg.setCost(reqMsg.getCost());

            MessageHelper.sendPacket(serverId, packet.getPlayerId(),
                    YanQuMessageUtils.buildMessage(GameProtocol.S_MUSIC_ROOM_RANK_JOIN_CROSS_BACK, respMsg.build()));

            if (respMsg.getRet() == 0) {
                MessageHelper.sendPacketToActivityAll(ClientProtocol.U_MUSIC_ROOM_RANK_NEED_UPDATE_NOTIFY,
                        MusicRoomRankProto.MusicRoomRankNeedUpdateNotifyMsg.newBuilder().setActivityId(reqMsg.getActivityId()).setRound(reqMsg.getRound()),
                        activity.getConfig().getActivityInfo());
            }
        }

    }

    @Override
    public void sendData() {

    }
}
